package com.haitonghop.requester;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.haitonghop.data.HaiBLMetalData;
import com.haitonghop.data.HaiBLModule;
import com.haitonghop.network.Request;
import com.haitonghop.network.RequestListener;
import com.haitonghop.network.UrlHelper;
import com.haitonghop.utils.AppSettingParams;
import com.haitonghop.utils.Logger;

public class OhVuiRequester extends Request{

	public static final String HOT = "hot";
	public static final String NEW = "new";
	public static final String LEGEND = "legend";
	public static final String VOTE = "vote";

	private String method = "";
	private int page = 1;
	
	private RequestListener requestListener;
	
	//Data
	ArrayList<HaiBLMetalData> haiVLMetalDatas = new ArrayList<HaiBLMetalData>();
	
	public void setMethod(String aMethod){
		this.method = aMethod;
	}
	
	public void setPage(int page){
		this.page = page;
	}
	
	public void setRequestListener(RequestListener requestListener){
		this.requestListener = requestListener;
	}
	
	
	public static final OhVuiRequester getNew(int page, RequestListener requestListener) {
		OhVuiRequester requester = new OhVuiRequester();
		requester.setMethod(NEW);
		requester.setPage(page);
		requester.setRequestListener(requestListener);
		return requester;
	}

	public static final OhVuiRequester getHot(int page, RequestListener requestListener) {
		OhVuiRequester requester = new OhVuiRequester();
		requester.setMethod(HOT);
		requester.setPage(page);
		requester.setRequestListener(requestListener);
		return requester;
	}

	public static final OhVuiRequester getLegend(int page, RequestListener requestListener) {
		OhVuiRequester requester = new OhVuiRequester();
		requester.setMethod(LEGEND);
		requester.setPage(page);
		requester.setRequestListener(requestListener);
		return requester;
	}
	
	public static final OhVuiRequester getVote(int page, RequestListener requestListener) {
		OhVuiRequester requester = new OhVuiRequester();
		requester.setMethod(VOTE);
		requester.setPage(page);
		requester.setRequestListener(requestListener);
		return requester;
	}

	@Override
	public void cancelRequest() {
		// TODO Auto-generated method stub		
	}

	@Override
	public void runRequest() {
		// TODO Auto-generated method stub
		url = UrlHelper.getUrlOfMethod(UrlHelper.BASE_URL_OHVUI,
				new String[]{"method","page"               ,"count"},
				new String[]{ method , String.valueOf(page),String.valueOf(AppSettingParams.COUNT)});
		send();
	}
	
	public ArrayList<HaiBLMetalData> getDataList(){
		return haiVLMetalDatas;
	}
	
	@Override
	public void requestFinished(JSONObject jsonObject) {
		if(jsonObject != null && jsonObject.has("data")){
			try {
				JSONArray jsonArray = jsonObject.getJSONArray("data");
				final int size = jsonArray.length();
				for(int i = 0; i < size ; i++){					
					JSONObject datas = jsonArray.getJSONObject(i);
					
					String imageUrl = datas.has(HaiBLMetalData.SRC) ? datas.getString(HaiBLMetalData.SRC) : "";
					String detailUrl = datas.has(HaiBLMetalData.IMG) ? datas.getString(HaiBLMetalData.IMG) : "";
					String title = datas.has(HaiBLMetalData.TITLE) ? datas.getString(HaiBLMetalData.TITLE) : "";
					
					String uploader = datas.has(HaiBLMetalData.UPLOADER) ? datas.getString(HaiBLMetalData.UPLOADER) : "";
					String viewCount = datas.has(HaiBLMetalData.VIEW_COUNT) ? datas.getString(HaiBLMetalData.VIEW_COUNT) : "0";
					String commentCount = datas.has(HaiBLMetalData.COMMENT_COUNT) ? datas.getString(HaiBLMetalData.COMMENT_COUNT) : "0";
					String likeCount = datas.has(HaiBLMetalData.LIKE_COUNT) ? datas.getString(HaiBLMetalData.LIKE_COUNT) : "0";
					String videoLink = datas.has(HaiBLMetalData.VIDEO_LINK) ? datas.getString(HaiBLMetalData.VIDEO_LINK) : "";
					String bigImageUrl = datas.has(HaiBLMetalData.BIG_SRC) ? datas.getString(HaiBLMetalData.BIG_SRC) : "";
					int mVideo = datas.has(HaiBLMetalData.VIDEO) ? datas.getInt(HaiBLMetalData.VIDEO) : 0;
					boolean video = mVideo == 1;
					
					HaiBLMetalData haiBLMetalData = new HaiBLMetalData(imageUrl, detailUrl, title, uploader, viewCount, commentCount);
					haiBLMetalData.setLikeCount(likeCount);
					haiBLMetalData.setTYPE(HaiBLModule.OHVUI);
					haiBLMetalData.setBigImageUrl(bigImageUrl);
					haiBLMetalData.setVideo(video);
					haiBLMetalData.setVideoLink(videoLink);
					
					haiVLMetalDatas.add(haiBLMetalData);
				}
				
				if(requestListener != null){
					requestListener.onRequestComplete(this);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void requestFailed(int errorCode, String message) {
		// TODO Auto-generated method stub
		if(requestListener != null){
			requestListener.onRequestFailed(message, this);
		}
	}

}
